import { ConvertComponentNameOptions } from '../types/utils'

export function camelize (str: string): string {
  str = String(str)
  return str.replace(/-(\w)/g, function (m, c) {
    return c ? c.toUpperCase() : ''
  })
}

export function kebab (str: string): string {
  str = String(str)
  return str.replace(/([A-Z])/g, '-$1').toLowerCase()
}

export function convertComponentName (componentName: string, { prefix = '', firstUpperCase = false }: ConvertComponentNameOptions): string {
  const pureName = componentName.replace(/^pi-/i, '')
  let camelizeName = `${camelize(`${prefix}${pureName}`)}`
  if (firstUpperCase) {
    camelizeName = camelizeName.charAt(0).toUpperCase() + camelizeName.slice(1)
  }
  return camelizeName
}

/**
 * @description: base64 to blob
 */
export function dataURLtoBlob (base64Buf: string): Blob {
  const arr = base64Buf.split(',')
  const typeItem = arr[0]
  const mime = typeItem.match(/:(.*?);/)![1]
  const bstr = atob(arr[1])
  let n = bstr.length
  const u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new Blob([u8arr], { type: mime })
}

/**
 * img url to base64
 * @param url
 */
export function urlToBase64 (url: string, mineType?: string): Promise<string> {
  return new Promise((resolve, reject) => {
    let canvas: any = document.createElement('CANVAS') as Nullable<HTMLCanvasElement>
    const ctx = canvas!.getContext('2d')

    const img = new Image()
    img.crossOrigin = ''
    img.onload = function () {
      if (!canvas || !ctx) {
        reject(new Error('0'))
        return
      }
      canvas.height = img.height
      canvas.width = img.width
      ctx.drawImage(img, 0, 0)
      const dataURL = canvas.toDataURL(mineType || 'image/png')
      canvas = null
      resolve(dataURL)
    }
    img.src = url
  })
}

export function openWindow (
  url: string,
  opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }
) {
  const { target = '__blank', noopener = true, noreferrer = true } = opt || {}
  const feature: string[] = []

  noopener && feature.push('noopener=yes')
  noreferrer && feature.push('noreferrer=yes')

  window.open(url, target, feature.join(','))
}

/**
 * @param {Object} params
 * @return {String} key1=value&key2=value2
 */
export function transformParams (params) {
  let result = ''
  const keys = Object.keys(params)
  const len = keys.length - 1
  const splitStr = '&'
  keys.forEach((key, index) => {
    result += `${key}=${params[key]}${len > index ? splitStr : ''}`
  })
  return result
}
